package cs275.ants.graph;

import java.util.LinkedList;
import java.util.List;

import com.google.common.collect.Lists;

import cs275.ants.graphics.Particle;

//import java.util.Date;
//import java.util.Random;

public class MazeCell {
	
	public enum Wall {
		LEFT("L"), UP("U"), RIGHT("R"), DOWN("D"), NONE("_");
		
		private String abbreviation;

		Wall(String abbreviation) {
			this.abbreviation = abbreviation;
		}
		
		public String getAbbrevitaion() {
			return abbreviation;
		}
		
		public Wall getOpposite() {
			switch(this) {
				case LEFT:
					return RIGHT;
				case RIGHT:
					return LEFT;
				case UP:
					return DOWN;
				case DOWN:
					return UP;
				default:
					return NONE;
			}
		}
	}
	private Particle particle;
	private boolean hasFood = false;
	
    public Particle getParticle() {
		return particle;
	}
    
    public void setHasFood(boolean food) { 
    	this.hasFood = food; 
    }
    
    public boolean hasFood() { 
    	return this.hasFood;
    }

	public void setParticle(Particle particle) {
		this.particle = particle;
	}

	public static int kCellSize = 10;
    public static int kPadding = 5;

	public List<Wall> walls; 
    public int row;
    public int column;
    
	// Visible for testing
    public MazeCell(int row, int column, Wall... walls) {
		this.row = row; 
		this.column = column;
    	LinkedList<Wall> wallsList = Lists.newLinkedList();
    	for(Wall wall : walls) {
    		wallsList.add(wall);
    	}
    	this.walls = wallsList;
    }
    
    public MazeCell() {
    	this(0, 0, Wall.LEFT, Wall.UP, Wall.RIGHT, Wall.DOWN);
    }

//    private int width;
//    private int height;
    
//    private static long seed = (new Date()).getTime();
//    public static Random generator = new Random(MazeCell.seed);
    
//    public MazeCell(int width, int height) {
//		this.width = width;
//		this.height = height;
//	}

	public boolean hasAllWalls() {
       return !walls.contains(Wall.NONE);
    }
	
	public boolean hasWall(Wall wall) { 
		return walls.contains(wall);
	}

    public void knockDownWall(Wall wall)
    {
       walls.set(walls.indexOf(wall), Wall.NONE);
    }
    
    public void knockDownWall(MazeCell adjCell)
    {
       // find adjacent wall
       Wall theWallToKnockDown = findAdjacentWall(adjCell);
       knockDownWall(theWallToKnockDown);
       
       adjCell.walls.set(adjCell.walls.indexOf(theWallToKnockDown.getOpposite()), Wall.NONE);
    }


    public Wall findAdjacentWall(MazeCell theCell)
    {
       if (theCell.row == row)
       {
          if (theCell.column < column)
             return Wall.LEFT;
          else
             return Wall.RIGHT;
       }
       else // columns are the same
       {
          if (theCell.row < row)
             return Wall.UP;
          else
             return Wall.DOWN;
       }
    }
    
//    public int GetRandomWall()
//    {
//    	int nextWall = generator.nextInt(3);
//       
//    	while ( (walls[nextWall] == 0)
//    			|| ((row == 0) && (nextWall == 0)) 
//    			|| ((row == height - 1) && (nextWall == 2)) 
//    			|| ((column == 0) && (nextWall == 1)) 
//    			|| ((column == width - 1) && (nextWall == 3))
//              ) {
//    		nextWall = generator.nextInt(3);
//    	}
//
//       return nextWall;
//    }

//    public void Draw(Graphics g)
//    {
//       if (Walls[0] == 1)
//       {
//          g.DrawLine(Pens.Blue, Row*kCellSize + kPadding, Column*kCellSize + kPadding, (Row+1) * kCellSize   + kPadding, Column*kCellSize +  + kPadding);
//       }
//       if (Walls[1] == 1)
//       {
//          g.DrawLine(Pens.Blue, Row*kCellSize  + kPadding, Column*kCellSize + kPadding, Row*kCellSize + kPadding, (Column+1)*kCellSize + kPadding);
//       }
//       if (Walls[2] == 1)
//       {
//          g.DrawLine(Pens.Blue, Row*kCellSize + kPadding, (Column+1)*kCellSize + kPadding, (Row+1)*kCellSize + kPadding, (Column+1)*kCellSize + kPadding);
//       }
//       if (Walls[3] == 1)
//       {
//          g.DrawLine(Pens.Blue,(Row+1)*kCellSize + kPadding , Column*kCellSize + kPadding, (Row+1)*kCellSize + kPadding, (Column+1)*kCellSize + kPadding);
//       }
//    }
    
    public String toDebugString() {
    	StringBuilder builder = new StringBuilder();
    	builder.append("[");
		for (Wall wall : walls) {
			builder.append(wall.getAbbrevitaion());
    	}
		builder.append("]");
		return builder.toString();
    }
}
